﻿using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.CMS.Administrator
{
    public partial class Default : Marbles.CMS.Core.AdministratorPageBase
    {

        public string CurrentContent
        {
            get
            {
                string value = Request.Params["action"];
                if (string.IsNullOrEmpty(value)) { return "ArticleList"; }
                return value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            LoadCurrentContent();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Admin)this.Master).MenuItemClicked += new MenuEventHandler(Default_MenuItemClicked);
        }

        protected void Default_MenuItemClicked(object sender, MenuEventArgs e)
        {
            string action = string.Empty;

            switch (e.Item.Value.ToString().ToLower())
            {
                case "articles":
                    action = "ArticleList";
                    break;
                case "explorer":
                    action = "Explorer";
                    break;
                case "newuser":
                    action = "User";
                    break;
                case "users":
                    action = "UserList";
                    break;
                default:
                    action = e.Item.Value;
                    break;
            }

            if (action != string.Empty) { Response.Redirect("?action=" + action); }
        }

        private void LoadCurrentContent()
        {
            string[] args = CurrentContent.Split('.');

            if (args.Length > 1)
            {
                switch (args[0].ToLower())
                {
                    case "edit": this.LoadUserControl(string.Format("/ContentTypes/{0}/Editor.ascx", args[1]), "ctlContent", plContent); break;
                }
            }
            else
            {
                this.LoadUserControl(string.Format("/Administrator/Content/{0}.ascx", CurrentContent), "ctlContent", plContent);
            }
        }
    }
}
